﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class OrthoCameraRatioExpand : MonoBehaviour
{
    public int ReferenceWidth;
    public int ReferenceHeight;
    public float ReferenceSize;

    Camera _camera;
    float _referenceAspectRatio;

    void OnEnable()
    {
        _camera = GetComponent<Camera>();
        _referenceAspectRatio = 1.0f * ReferenceWidth / ReferenceHeight;
        CalculateSize();
    }

#if UNITY_EDITOR
    void Update()
    {
        CalculateSize();
    }
#endif


    void CalculateSize()
    {
        float screenFov = 1.0f * Screen.width / Screen.height;
        float referenceSize = 1.0f * ReferenceWidth / ReferenceHeight;
        if (screenFov >= _referenceAspectRatio)
        {
            _camera.aspect = screenFov;
            _camera.orthographicSize = ReferenceSize;
        }
        else
        {
            _camera.aspect = screenFov;
            _camera.orthographicSize = ReferenceSize * (referenceSize / screenFov);
        }
    }
}
